package com.xmall.msg.handler;

import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.xmall.config.AmqpCommonConfig;
import com.xmall.msg.entity.dict.DictUpdateMsg;
import com.xmall.utils.DictUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 取消订单
 */
@Slf4j
@Component
@RabbitListener(queues = "#{dictQueue.name}", containerFactory = AmqpCommonConfig.RABBIT_LISTENER_CONTAINER_FACTORY_NAME)
public class DictUpdateHandler {

    @RabbitHandler
    public void msg(Channel channel, Message message, DictUpdateMsg msg) throws IOException {
        log.info("rabbitmq handler: common service: DictUpdateHandler: {}", JSONUtil.toJsonStr(msg));
        try {
            DictUtils.updateDict(msg.getDictDto());
        }catch (Exception e) {}
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
